﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WQLWebAPI.Api.Configuration.Consts;
using WQLWebAPI.EntityFramework;

namespace WQLWebAPI.Api.Configuration
{
    public static class DataBaseConfig
    {
        public static void ConfigDataBase(this IServiceCollection services, IConfiguration configuration)
        {
            var databaseType = configuration.GetSection("Settings")["DatabaseType"];
            if (databaseType.ToLower() == DatabaseConst.Mysql)
            {

                string connectionString = configuration.GetConnectionString(DatabaseConst.MySqlConnection);
                services.AddDbContext<ApplicationDbContext>(options =>
                {
                    options.UseMySql(connectionString, sql => { sql.ServerVersion(new Version(5, 5, 47), ServerType.MySql); });


                    //options.UseSqlServer(connectionString);
                });
                //注册asp.net Identity
                services.ConfigIdentityByMysql(configuration);

            }
            else
            {
                string connectionString = configuration.GetConnectionString(DatabaseConst.SqlServerConnection);
                services.AddDbContext<ApplicationDbContext>(options =>
                {
                    options.UseSqlServer(connectionString);
                    //options.UseSqlServer(connectionString);
                });
                //注册asp.net Identity
                services.ConfigIdentityBySqlServer();

            }


        }
    }
}
